<?php
	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_ACTION_PATH."base_action_class.php");
    require_once(CUTEENGINE_BASE_STEP_PATH."base_step_class.php");
	//
    // t_action_execute_query
    //
	class t_action_execute_query extends t_base_action
	{
        // property declaration
		// protected        
		// G
		const language_position_last = 1;

		// property declaration
		// public
		// P
        const proper_properties_position_per_query = 0;
        const proper_properties_position_per_parameters_names = 1;
        const proper_properties_position_per_parameters_put_result_into_url_parameter = 2;
        const proper_properties_position_per_is_executed_from_event = 3;
        const proper_properties_position_last = 4;
        // R
        const required_fields_count = 1;
      
        // method declaration
    	// protected
		// C
        protected function custom_execute_step($a_step)        
        {
            
			$local_field = $a_step->field;
            if ($this->is_executed_from_event())
            {
                $local_main_tag = $local_field->get_executor_tag();
               // print_r($local_main_tag);
			   //echo "local_main_tag: ".$local_main_tag->name."<br>";
                $local_class = get_class($a_step);
                //echo "local_class:$local_class<br>";
				$local_field_class = get_class($local_field);
                switch ($local_class) 
                {
                    /*********** t_step_set_ce_field_event ***********/
                    case t_step_set_ce_field_event:
                        if ($local_field_class != 't_field_processes_engine')
						{
							$local_field->add_event($local_field->get_event_type($this->get_action_name()), $local_field->get_event_name($local_field->get_event_type($this->get_action_name())),
								"var local_parent = ".t_cuteengine::javascript_instance_name."['".$local_field->name."'];
			if (!local_argoument)
				var local_argoument = local_parent.prepare_childs_values_to_send('".$local_main_tag->name."');
			else	
				local_argoument = local_parent.prepare_childs_values_to_send('".$local_main_tag->name."',local_argoument);
			if (local_argoument === false)
				return false;
			".$this->get_content_loader_code('',true,true));
						}
                        
                    break;		
                }
            }    
        }
        // G
		protected function get_argument_index()
		{
			
            if ((($this->is_executed_from_event()) || ($this->main_action != null)) /*&& 
				($local_class != 't_field_do_it')*/)
                $local_return = 3;
            else
                $local_return = -1;
            return $local_return;    
                
		}  
		protected function get_parameters_array($a_parameter)
		{
			global $g_ambient_namespace;
   			//print_r($a_parameter);
			$local_index = $this->get_argument_index();
			//echo "PARAMETER: ". $g_ambient_namespace->remove_hide_separator($a_parameter->get_argument_value($local_index))." - local_index:$local_index<br>";  
			if ($local_index == -1)
                $local_return = $g_ambient_namespace->remove_hide_separator(explode(t_url_parameter::separator_level_0,$a_parameter->get_argument_value($local_index)));
            else
                $local_return = $g_ambient_namespace->remove_hide_separator(explode(t_url_parameter::separator_level_1,$a_parameter->get_argument_value($local_index)));
            return $local_return;    
                
		} 		
		
		protected function get_required_fields_count()
        {
            return self::required_fields_count;
        }
        

		// I
        protected function initialize_action()
        {		

			if (! $this->initialized)
			{
                parent::initialize_action();
				
				
                //$this->initialized = true;

			}
			
			return $this->initialized;
		}		
        protected function initialize_step_execution()
        {		
			if ($this->current_step_index == t_base_action::step_start)
            {
                $local_step = $this->steps_list[$this->current_step_index];
                $local_step->initialize();
				
                $this->custom_execute_step($local_step); 
            }    
			
		}	
		protected function is_executed_from_event()
		{
			return (strtoupper($this->proper_properties_array[self::proper_properties_position_per_is_executed_from_event]) == 'TRUE');
		}	
        // method declaration
    	// public
        public function __construct($a_parent_component,$a_parent_part,$a_common_properties_str, $a_proper_properties_str) 
		{
            parent::__construct($a_parent_component,$a_parent_part,$a_common_properties_str, $a_proper_properties_str);
            //echo "count:".count($this->fields_list);
			
			// steps
            $this->steps_list[t_base_action::step_start] = t_base_step::create_step_from_string('|t_step_set_ce_field_event|'.$this->get_action_name().'_get|'.
              $this->fields_list[t_base_action::step_start].'|');  
            $this->steps_list[t_base_action::step_criteria] = null;   
			$this->steps_list[t_base_action::step_result] = null;   
			
		}
        // D
		public function do_action_from_ajax($a_json_result = null,$a_connection = null)
        {
			//echo __METHOD__ . "<br>"; 
			//echo "this->get_action_name(): ".$this->get_action_name()."<br>";
			
			global $g_ambient_namespace;
			if ($a_connection == null)
				$local_connection = $g_ambient_namespace->connection;
			else
				$local_connection = $a_connection;
            if ($this->is_executed_from_event() && (!isset($a_json_result)))
                $a_json_result = new t_content_loader_result;
			//
			$local_field = $this->steps_list[t_base_action::step_start]->field;
			$local_class = get_class($local_field);
			
            $local_component = $this->parent_component;
			$local_parameter = parent::do_action_from_ajax();
			//print_r($local_parameter);
			$local_query_name = $this->get_action_name()."_query";
            $local_query_query = trim($this->proper_properties_array[self::proper_properties_position_per_query]);
            if ($local_query_query == '')
                return;
            $local_component->new_query(
				$local_query_name,
				$local_query_query,
				$local_connection);
			//echo "local_query_query: $local_query_query<br>";	
           // $local_parameters_names = explode(t_strings_utility_namespace::separator_level_2,
             
            $local_parameters_names = $this->proper_properties_array[self::proper_properties_position_per_parameters_names];    
			//print_r($local_parameters_names);
            $local_put_result_into_url_parameter = strtoupper($this->proper_properties_array[self::proper_properties_position_per_parameters_put_result_into_url_parameter]);    
			// GET QUERY
			$local_query_query = $local_component->get_query(
				$local_query_name);
			$local_query_query->reset_parameters();
			// COOKIE
			if (method_exists($local_field,'set_cookie_value'))
				$local_field->set_cookie_value($local_query_query);
			// PARAMETERS
			$local_parameters_array = $this->get_parameters_array($local_parameter);
			//print_r($local_parameters_array);
			foreach ($local_parameters_names as $local_parameter_name_index=>$local_parameter_name) 
			{
				//echo "local_parameter_name: $local_parameter_name ";
                $local_db_field_index = $local_query_query->get_parameter_index_by_name($local_parameter_name);
				if ($local_db_field_index >= -1)
				{
					//echo "index: $local_db_field_index - value:".$local_parameters_array[$local_parameter_name_index];
					$local_query_query->set_value_parameter(
						$local_parameters_array[$local_parameter_name_index],$local_db_field_index);
					//echo "SQL: ".$local_query_query->retrive_sql_query_with_value_parameter($local_query_query->sql_str);
					if ($local_query_query->is_not_correct_value($local_db_field,$local_parameters_array[$local_parameter_name_index],$local_db_field_index))
						if($local_invalid_value_list == '')
							$local_invalid_value_list = "Fields values ivalid for: `$local_db_field->name`";
						else
							$local_invalid_value_list .= ", `$local_db_field->name`";
				}			
			}	
			//echo "local_invalid_value_list: $local_invalid_value_list<br>";

			if ($local_invalid_value_list != '')
			{
				$a_json_result->error_msg  = $local_invalid_value_list;
			}	
			else
			{
			
				$a_json_result->succeeded = $local_query_query->execute_query();
					//echo $local_query_query->retrive_sql_query_with_value_parameter($local_query_query->sql_str);
				$a_json_result->query = $local_query_query;
				//print_r($a_json_result);
				$local_field = $this->steps_list[t_base_action::step_start]->field;
				//echo "get_class(local_field): ". get_class($local_field) ."<br>";
				
				if ((get_class($local_field)) == 't_field_processes_engine')
				{
					if ($a_json_result->succeeded === false)
					{
						$local_field->apply_error_result_script($a_json_result);
						
					}	
					else
					{
						$local_field->apply_result_script($a_json_result);
					}
					//echo $a_json_result->get_json_string();					
				}
				else
				{
					if (! $a_json_result->succeeded)
					{
						
						//echo "ERORRE<br>";
						$a_json_result->error_msg .= $local_query_query->get_last_error();
						
						if ($local_class == 't_field_memory_container')
						{
							
							$local_field->apply_error_result_script($local_query_query,-1,0,array($this,$a_json_result));
						}
					}	
					else
					{
						/*if ($this->get_action_name() == 'set_set_invii_stato')
			{
				echo $this->get_action_name() . " - this->steps_list[t_base_action::step_start]->field - ".get_class($this->steps_list[t_base_action::step_start]->field)."<br>";
				echo $local_query_query->retrive_sql_query_with_value_parameter($local_query_query->sql_str);
			}*/
						
						//echo "local_class: $local_class<br>";
						if (($local_class == 't_field_do_it') || ($local_class == 't_field_memory_container') || ($local_class == 't_field_login') || ($local_class == 't_field_redirect'))
						{
			
								
								$local_query_query->execute_function_from_even_with_empty_query_result($local_field,"apply_result_script",
									array($this,$a_json_result));
						}
						// if requested, the query result is put into url as argouments
						if ($local_put_result_into_url_parameter === 'TRUE')
							$local_query_query->execute_function_from_query_result($local_query_query,"put_query_result_into_url_parameter",$local_parameter);
						
					}
				}	
				
				$local_query_query->close_all_results();
				if ($this->is_executed_from_event())
				{
				    if ($a_json_result->succeeded)
					{
						$this->execute_child_ations($a_json_result,$local_query_query->connection);
					}
					echo $a_json_result->get_json_string(); 
                }					
			}
			$local_query_query->close_all_results();
        }
        // G
        public function get_language_position_last()
        {
            return self::language_position_last;
        }
		static function get_help_proper_property($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_proper_property($a_index)
		{
			$local_return = '';
			//echo "a_index: $a_index<br>";
			/*switch ($a_index) 
            {
				case self::proper_properties_position_per_:
					$local_return = '';
				break;				
			}*/
			return $local_return;
		}		
        protected function get_proper_properties_position_last()
        {
            return self::proper_properties_position_last;
        }
		// I
		public function is_executable()
		{
			return ($this->is_executed_from_event());
		}

    }
?>    